package com.enterprise.util;

import com.enterprise.entity.News;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
 * 线程工具类
 * @author dwb
 * @version
 */
public class ThreadUtil {

	private Log log = LogFactory.getLog(ThreadUtil.class);

	private static ThreadUtil eUtil;

	private ExecutorService executor;

	public ThreadUtil() {
		if (executor == null) {
			executor = Executors.newFixedThreadPool(10, new ScheduleThreadFactory(ThreadUtil.class.getSimpleName()));
		}
	}

	/**
	 * 获取发送器
	 * 
	 * @return
	 */
	public static ThreadUtil getInstance() {
		if (eUtil == null) {
			synchronized (ThreadUtil.class) {
				if (eUtil == null) {
					eUtil = new ThreadUtil();
				}
			}
		}
		return eUtil;
	}

	/**
	 * 线程创建工厂
	 */
	class ScheduleThreadFactory implements ThreadFactory {

		private int counter = 0;
		private String prefix = "";

		public ScheduleThreadFactory(String prefix) {
			this.prefix = prefix;
		}

		public Thread newThread(Runnable r) {
			String treadName = prefix + "-" + counter++;
			return new Thread(r, treadName);
		}
	}

	/**
	 * 组合购买交易
	 * @param job
	 */
	public void buyComposeFundSync(News job) {
		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				log.info("执行组合购买交易线程");
			}
		};
		executor.execute(runnable);
	}
}
